Vue.component('deal-autocomplete', {
  template: `
        <el-autocomplete
            v-model="form.company_name"
            class="inline-input"
            :fetch-suggestions="companyQuerySearch"
            placeholder="公司名称"
            :trigger-on-focus="false"
            :maxlength="100"
            @select="companyHandleSelect"
        >
            <template v-if="dataId" slot="append">
              <a @click="companyHandleSelect">查看标的公司信息</a>
            </template>
        </el-autocomplete>
    `,
  props: {
    form: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      tycParam: {},
      dataId: undefined,
      trigger: true
    }
  },
  created() {
    this.dataId = window.parent.dataId
    this.form.company_id = ""
    this.form.company_name = ""
    this.form.manage_company_name = ""
    this.form.manage_company = ""
    this.form.issued = ""
    this.form.issued_name = ""
  },
  watch: {
    'form': {
      handler(val) {
        this.$nextTick(() => {
          if (this.form.id && this.trigger) {
            this.trigger = false
            axios({
              url: window.parent.baseUrl + '/company/companyBase/' + val.company_id,
              headers: {
                'Content-Type': 'application/json;charset=utf-8',
                'Authorization': window.parent.token
              },
              method: 'get'
            }).then(res => {
              let _res = res.data
              if (_res.code === 200) {
                this.form.company_name = _res.data.companyName
              }
            })
          }
        })
      },
      deep: true
    }
  },
  methods: {
    companyQuerySearch(queryString, cb) {
      axios({
        url: window.parent.baseUrl + '/company/companyBase/companyListByName?companyName=' + queryString,
        headers: {
          'Content-Type': 'application/json;charset=utf-8',
          'Authorization': window.parent.token
        },
        method: 'get'
      }).then(res => {
        let _res = res.data
        if (_res.code === 200) {
          cb(_res.data)
        }
      })
    },
    // 标的公司选择后事件
    companyHandleSelect(item) {
      if (!this.dataId) {
        this.form.deal_name = this.form.company_name
        this.checkDealNameExist()
      }
      this.form.tycId = item.id
      axios({
        url: window.parent.baseUrl + '/base/formBase/getByFormKeyAndJoinTableKey',
        headers: {
          'Content-Type': 'application/json;charset=utf-8',
          'Authorization': window.parent.token
        },
        method: 'get',
        params: { formKey: 'deal_base', joinTableKey: 'company_base' }
      }).then(res => {
        let __res = res.data
        if (__res.data) {
          this.tycParam.id = this.form.id
          this.tycParam.companyName = this.form.company_name
          this.tycParam.type = 'deal'
          axios({
            url: window.parent.baseUrl + '/company/companyBase/companyDetailsByName',
            headers: {
              'Content-Type': 'application/json;charset=utf-8',
              'Authorization': window.parent.token
            },
            method: 'get',
            params: this.tycParam
          }).then(res => {
            let _res = res.data
            if (_res.code === 200) {
              if (_res.msg === 'exist') {
                this.form.company_name = ''
                this.$message({ showClose: true, message: '公司已存在,请重新录入！', type: 'error' })
              } else if (_res.msg === 'nofind') {
                this.form.company_name = ''
                this.$message({ showClose: true, message: '天眼查没有获取到该公司信息！', type: 'error' })
              } else {
                this.formCleaing()
                // 公司简称
                this.form.company_short_name = _res.data.companyShortName || _res.data.companyName
                // 公司名称
                this.form.company_name = _res.data.companyName
                // 企业统一社会信用代码
                this.form.credit_code = _res.data.creditCode
                // 法人代表
                this.form.legal_person = _res.data.legalPerson
                // 成立时间
                this.form.set_date = this.formatDate(_res.data.setDate)
                // 一级行业
                this.form.industry_first = _res.data.industryFirst
                this.form.industry_second = _res.data.industrySecond
                // this.$refs.industry_zj.initIndustrySecond()
                // 注册地-省
                this.form.province_registered = _res.data.provinceRegistered
                this.form.city_registered = _res.data.cityRegistered
                // this.$refs.cityZj.initCity()
                // 注册地-详细地址
                this.form.details_registered = _res.data.detailsRegistered
                // 注册资本金额
                this.form.capital_registered = _res.data.capitalRegistered
                // 注册资本币种
                this.form.currency = _res.data.currency
                // 经营范围
                this.form.business_scope = _res.data.businessScope
                // 天眼查ID
                this.form.tyc_id = _res.data.tycId
                // 公司logo
                this.form.logo_info = _res.data.logo
                // 企业简介
                this.form.remark = _res.data.remark
                this.$message({ showClose: true, message: '已获取工商信息！', type: 'success' })
              }
            }
          })
        }
      })

    },
    // 判断项目名称是否存在
    checkDealNameExist() {
      const dealName = this.form.deal_name
      if (dealName === '' || dealName === undefined) {
        // Notification.error({
        //   title: '请先输入项目名称！！'
        // })O
        this.$message({ showClose: true, message: '请先输入项目名称', type: 'error' })
        return
      }
      this.tycParam.dealName = dealName
      axios({
        url: window.parent.baseUrl + '/deal/dealBase/getDealBaseByName',
        headers: {
          'Content-Type': 'application/json;charset=utf-8',
          'Authorization': window.parent.token
        },
        method: 'get',
        params: this.tycParam
      }).then(res => {
        let _res = res.data
        if (_res.data !== null && _res.data.id !== this.dataId) {
          // this.$parent.error = '项目名称不能重复'
          this.$message({ showClose: true, message: '项目名称不能重复', type: 'error' })
          // this.rules.dealName = [{ required: true, message: this.$t('项目名称不能重复'), trigger: 'change' }]
          this.form.deal_name = undefined
        }
      })
    },
    formCleaing() {
      this.form.company_short_name = ''
      this.form.credit_code = ''
      this.form.legal_person = ''
      this.form.set_date = ''
      this.form.industry_first = ''
      this.form.industry_second = ''
      this.form.province_registered = ''
      this.form.city_registered = ''
      this.form.details_registered = ''
      this.form.capital_registered = ''
      this.form.business_scope = ''
      this.form.currency = ''
      this.form.tyc_id = ''
      this.form.logo_info = ''
    },
      formatDate(date) {
      // date = new Date();
      date = new Date(date); //转换成Data();
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      return y + '-' + m + '-' + d;
      }
  }
})
